package util;

import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件上传工具类
 *
 * @Author zqx
 * @Date 2023-11-24
 */
public class FileUploadUtil {
    /**
     * 1.设置文件上传大小
     */
    private long maxSize;

    /**
     * 2.设置允许上传文件的类型
     */
    private List<String> allowFileExt = new ArrayList<String>();

    /**
     * 设置默认容量和默认允许上传的文件
     */
    public FileUploadUtil() {
        // 默认允许上传1MB的文件 - 默认单位是字节
        this.maxSize = 1 * 1024 * 1024;
        this.setAllowFileExt("doc,jpg,png");
    }

    public long getMaxSize() {
        return maxSize;
    }

    public void setMaxSize(long maxSize) {
        this.maxSize = maxSize;
    }

    public List<String> getAllowFileExt() {
        return allowFileExt;
    }

    public void setAllowFileExt(String ext) {
        if (ext != null && !"".equals(ext)) {
            this.allowFileExt.clear();
        }
        String[] str = ext.split(",");
        for (String s : str) {
            this.allowFileExt.add(s);
        }
    }


    /**
     * 1.获取上传的原文件名 - abc.jpg
     *
     * @param header
     * @return
     */
    private String getFilename(String header) {
        String filename = "";
        int beginIndex = header.indexOf("filename=\"") + 10;
        int endIndex = header.lastIndexOf("\"");
        filename = header.substring(beginIndex, endIndex);
        return filename;
    }

    /**
     * 2.获取上传文件扩展名 .jpg
     *
     * @param filename
     * @return
     */
    private String getFileExt(String filename) {
        String value = "";

        if (filename == null || filename.lastIndexOf('.') != -1
                && filename.lastIndexOf('.') == filename.length() - 1) {
            return "";
        }

        int beginIndex = filename.lastIndexOf('.') + 1;
        int endIndex = filename.length();
        value = filename.substring(beginIndex, endIndex);

        return value;
    }

    /**
     * 3.上传文件，文件名为UUID
     *
     * @param part
     * @param path
     * @return
     */
    public String upload(Part part, String path) {
        String header = part.getHeader("Content-Disposition");
        String filename = this.getFilename(header);
        String ext = this.getFileExt(filename);
        String newFilename = UUIDUtil.createUUID() + "." + ext;
        return upload(part, path, newFilename);
    }

    /**
     * 上传文件
     *
     * @param part
     * @param path
     * @param flag flag为true,则文件为名UUID，flag为false，则文件名为原文件名
     * @return
     */
    public String upload(Part part, String path, boolean flag) {
        String header = part.getHeader("Content-Disposition");
        String filename = this.getFilename(header);
        String ext = this.getFileExt(filename);
        String newFilename = UUIDUtil.createUUID() + "." + ext;

        if (!flag) {
            return upload(part, path, newFilename);
        }
        return upload(part, path, filename);
    }

    /**
     * 上传文件，文件名由用户指定
     *
     * @param part
     * @param path
     * @param file
     * @return
     */
    public String upload(Part part, String path, String file) {
        // 判断是否允许上传文件容量大小
        if (part.getSize() > this.getMaxSize()) {
            throw new RuntimeException("上传文件容量溢出，只能上传" + this.getMaxSize() / 1024 * 1024 + "字节文件");
        }

        // 获取文件后缀
        String ext = this.getFileExt(file);
        // 判断是否允许上传文件类型
        if (!this.getAllowFileExt().contains(ext)) {
            throw new RuntimeException("上传失败，只能上传文件的类型有：" + this.getAllowFileExt().toString());
        }
        // 获取上传文件路径
        String uploadFile = path + File.separator + file;

        try {
            part.write(uploadFile);
        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败";
        }
        return file;
    }
}
